package cn.edu.suda.catalina.dataupdate.controller;

import cn.edu.suda.catalina.dataupdate.util.ReflectUtil;
import cn.edu.suda.catalina.dataupdate.util.ClassNameUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by admin on 2017/5/15.
 */
@RestController
public class TestController {
    private Map<String, List<Object>> resultMap;

    @RequestMapping("/config")
    public String config(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> parameters = parameterMap.keySet();

        resultMap = UploadController.result;
        Set<String> optionKeys = resultMap.keySet();
        String[] options = (String[]) resultMap.get("Option").toArray();

        int i;
        //遍历脚本
        for (String key : optionKeys) {
            if (!key.equals("Option")) {
                i = 0;
                //遍历参数
                for (String k : parameters) {
                    if (k.startsWith(key)) {
                        //获取对应下标
                        for (int n = 0; n < options.length; n++) {
                            if (options[n].equals(parameterMap.get(k)[0])) {
                                resultMap.get(key).set(i++, n);
                            }
                        }
                    }
                }
            }
        }
        return "配置成功！";
    }

    @RequestMapping("/test")
    public String test(HttpServletRequest request) {
        String javaFilePath = request.getServletContext().getRealPath("/scripts");
        String dataFilePath = request.getServletContext().getRealPath("/data");

        resultMap = UploadController.result;
        Set<String> optionKeys = resultMap.keySet();

        ExecutorService threadPool = Executors.newFixedThreadPool(10);

        for (String key : optionKeys) {
            threadPool.execute(() -> {
                if (!key.equals("Option")) {
                    String[] args = new String[resultMap.get(key).size()];
                    String[] options;
                    String line;
                    try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFilePath + File.separator + "data.txt"), "GBK"));
                        while ((line = reader.readLine()) != null) {
                            options = line.split("#");
                            //字段关联
                            for (int i = 0; i < args.length; i++) {
                                args[i] = options[(int) resultMap.get(key).get(i)];
                            }
                            new ReflectUtil(javaFilePath).run(ClassNameUtil.getFullName(key), args);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        return "测试成功！";
    }
}
